// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports

using System.Runtime.Serialization;


namespace SCIEF.SysBFW.CommonExt
{
	[Serializable()]public class ExtendedDataTable : DataTable
	{
		
#region  Fields
		private SqlDataAdapter _adapter = new SqlDataAdapter();
		private string _commandName;
		private MetaQuery metaQuery;
#endregion
		
#region  Methode
		/// <summary>
		///     Creates a new ExtendedDataTable by initializing it's adapter.
		///     Defines what data will be load into the DataTable and how it will
		///     be managed.
		/// </summary>
		/// <param name="adapter"></param>
		/// <remarks>The SqlDataAdapter should have a valid SelectCommand and include a rowversion  (alias timestamp) field</remarks>
		public ExtendedDataTable(SqlDataAdapter adapter) : this(adapter, adapter.SelectCommand.CommandText)
		{
		}
		/// <summary>
		///
		/// </summary>
		/// <remarks>Mandatory if we need to use GetChanges</remarks>
		public ExtendedDataTable()
		{
			SqlCommand command = Base.Connection.Command();
			this.Adapter = new SqlDataAdapter(command);
		}
		/// <summary>
		///     Creates a new ExtendedDataTable by initializing it's adapter.
		///     Defines what data will be load into the DataTable and how it will
		///     be managed.
		/// </summary>
		/// <param name="adapter"></param>
		/// <param name="command">MetaCommand (select)</param>
		/// <remarks>The "select" statement in the command parameter must include a timestamp (alias rowversion) field</remarks>
		public ExtendedDataTable(SqlDataAdapter adapter, string command)
		{
			this.CommandName = command;
			this.Adapter = adapter;
			//
			this.Adapter.SelectCommand.CommandText = this.CommandName;
			this.Adapter.AcceptChangesDuringUpdate = true; //Do not Remove... If Remove Can't trap OnUpdatedRow
			this.Adapter.Fill(this);
		}
		public ExtendedDataTable(string tableName) : base(tableName)
		{
			
		}
		public ExtendedDataTable(string tableName, string tableNamespace) : base(tableName, tableNamespace)
		{
			
		}
		protected ExtendedDataTable(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
		{
			
		}
		
		/// <summary>
		/// From the SELECT command generate the INSERT, UPDATE and DELETE command
		/// </summary>
		/// <remarks>Property must have a TStamp or RecordVersion</remarks>
		public void BuildCUDCommands(MetaQuery metaQuery)
		{
			//keep a copy of the original SELECT command. / Might be an SP Buy design
			string oldCmdText = this.Adapter.SelectCommand.CommandText;
			CommandType oldCmdType = this.Adapter.SelectCommand.CommandType;
			//Here we Assign Metaquery for updating RowVersion, later.
			this.metaQuery = metaQuery;
			this.Adapter.SelectCommand.CommandType = CommandType.Text;
			this.Adapter.SelectCommand.CommandText = metaQuery.SQLStatement;
			//To use the CompareRowVersion option, the "select" command must include
			//a rowversion (alias timestamp ) field.
			//Refer to :
			//If not, there will be errors when
			//trying to build the Delete/Insert/Update commands below. This field will
			//be use as aconcurrency indicator.
			//
			SqlCommandBuilder builder = new SqlCommandBuilder(this.Adapter);
			builder.ConflictOption = ConflictOption.CompareRowVersion;
			try
			{
				this.Adapter.DeleteCommand = builder.GetDeleteCommand(true);
				this.Adapter.InsertCommand = builder.GetInsertCommand(true);
				this.Adapter.UpdateCommand = builder.GetUpdateCommand(true);
			}
			catch (Exception ex)
			{
				throw (new Exception("Error in BuildCUDCommands MetaQuery: " + metaQuery.QueryName, ex));
			}
			finally
			{
				//Reassigne the original SQL command
				this.Adapter.SelectCommand.CommandType = oldCmdType;
				this.Adapter.SelectCommand.CommandText = oldCmdText;
			}
		}
#endregion
		
#region  Properties
		public SqlDataAdapter Adapter
		{
			get
			{
				return _adapter;
			}
			set
			{
				_adapter = value;
			}
		}
		/// <summary>
		/// Select command
		/// </summary>
		/// <value></value>
		/// <returns></returns>
		/// <remarks></remarks>
		public string CommandName
		{
			get
			{
				return _commandName;
			}
			set
			{
				_commandName = value;
			}
		}
#endregion
		
	}
	
}
